package index;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

import lemmatisation.SnowballProgram;
import lemmatisation.testLemmatisation;

public class TextFilter {

	// mots vides du français
	public static final String[] STOP_WORDS = {"a","à","afin","ai","aie","aient","aient","ainsi","ais","ait","alors","as","assez","au","auquel","auquelle","aussi","aux","auxquelles","auxquels","avaient","avais","avait","avant","avec","avoir","beaucoup","ca","ça","car","ce","cela","celle","celles","celui","certain","certaine","certaines","certains","ces","cet","cette","ceux","chacun","chacune","chaque","chez","ci","comme","comment","concern","concernant","connait","connaît","conseil","contre","d","dans","de","des","desquelles","desquels","differe","different","différent","differente","différente","differentes","différentes","differents","différents","dois","doit","doivent","donc","dont","du","dû","duquel","dus","e","elle","elles","en","encore","ensuite","entre","es","est","et","etai","etaient","étaient","etais","étais","etait","était","etant","étant","etc","ete","été","etiez","étiez","etion","etions","étions","etre","être","eu","eux","evidenc","evidence","évidence","expliqu","explique","fai","faire","fais","fait","faite","faites","faits","fera","feras","fini","finie","finies","finis","finit","font","grace","grâce","ici","il","ils","intere","interessant","intéressant","interesse","intéressé","j","jamais","je","l","la","laquell","laquelle","le","lequel","les","lesquelles","lesquels","leur","leurs","lors","lorsque","lui","m","ma","mainten","maintenant","mais","mal","me","meme","même","memes","mêmes","mes","mettre","moi","moins","mon","n","ne","ni","no","non","nos","notre","nôtre","notres","nôtres","nou","nous","obtenu","obtenue","obtenues","obtenus","on","ont","or","ou","où","par","parfois","parle","pars","part","pas","permet","peu","peut","peuvent","peux","plus","pour","pourquo","pourquoi","pouvez","pouvons","prendre","pres","près","princip","principal","principaux","qu","quand","que","quel","quelle","quelles","quelques","quels","qui","quoi","sa","savoir","se","seront","ses","seul","seuls","si","soient","soit","son","sont","sous","souvent","sui","suis","sur","t","ta","te","tel","telle","telleme","tellement","telles","tels","tes","ton","toujour","toujours","tous","tout","toute","toutes","traite","tres","très","trop","tu","unv","une","unes","uns","utilise","utilisé","utilisee","utilisée","utilisées","utilisees","uilisés","utilises","va","venir",
		"vers","veut","veux","vont","voulez","voulu","vous"};


	Hashtable Stoptable;
	
	public TextFilter() {
		// TODO Auto-generated constructor stub
		
		Stoptable= new Hashtable<String,String>();
		for (int i=0; i<STOP_WORDS.length; i++)
		{
			Stoptable.put(STOP_WORDS[i],STOP_WORDS[i]);
		}

	}
	
	/**
	  * Permet de filtrer les termes du le texte.
	  */
	  public String[] filtreTerme (String texte) {

	  texte= texte.toLowerCase();
	  				
	  //nouveau filtrage des terme
	  //on retire les mots vides
	  Enumeration e = Stoptable.elements();
	  while(e.hasMoreElements()) {
	  	texte=texte.replaceAll(" "+(String) e.nextElement() +" ", "_" );
	  }

	  texte=texte.replaceAll("\'", "\'_" );
	  texte=texte.replaceAll("[ \t\nB\f\r]", "_" );
	  texte=texte.replaceAll("ee", "e_e" );
	  texte=texte.replaceAll("s\'", "_s\'" );
	  texte=texte.replaceAll("d\'", "_d\'" );
	  texte=texte.replaceAll("qu\'", "_qu\'" );
	  texte=texte.replaceAll("esl", "es_l" );
	  texte=texte.replaceAll("ssc", "s_sc" );
	  texte=texte.replaceAll("ual", "u_al" );

		
	//// on commence par remplacer
	//texte=texte.replace('.',' ');
	//texte=texte.replace('/',' ');
	//texte=texte.replace('!',' ');
	//texte=texte.replace(';',' ');
	//texte=texte.replace(',',' ');
	//texte=texte.replace('+',' ');
	//texte=texte.replace('*',' ');
	//texte=texte.replace('-',' ');
	//texte=texte.replace('?',' ');
	//texte=texte.replace('[',' ');
	//texte=texte.replace(']',' ');
	//texte=texte.replace('(',' ');
	//texte=texte.replace(')',' ');
	//texte=texte.replace('\'',' ');
	//texte=texte.replace('\"',' ');
	//texte=texte.replace(':',' ');
	//texte=texte.replace('\\',' ');
	//texte=texte.replace('}',' ');
	//texte=texte.replace('{',' ');
	//texte=texte.replace('&',' ');
	////texte=texte.replace('©',' ');
//					
	//String[] mots=texte.split(" ");

	  				
	  //on commence par remplacer
	  texte=texte.replace('.','_');
	  texte=texte.replace('/','_');
	  texte=texte.replace('!','_');
	  texte=texte.replace(';','_');
	  texte=texte.replace(',','_');
	  texte=texte.replace('+','_');
	  texte=texte.replace('*','_');
	  texte=texte.replace('-','_');
	  texte=texte.replace('?','_');
	  texte=texte.replace('[','_');
	  texte=texte.replace(']','_');
	  texte=texte.replace('(','_');
	  texte=texte.replace(')','_');
	  //texte=texte.replace('\'','_');
	  texte=texte.replace('\"','_');
	  texte=texte.replace(':','_');
	  texte=texte.replace('\\','_');
	  texte=texte.replace('}','_');
	  texte=texte.replace('{','_');
	  texte=texte.replace('&','_');
	  texte=texte.replace('<','_');
	  texte=texte.replace('>','_');
	  texte=texte.replace('�','_');
	  texte=texte.replace('�','_');
	  texte=texte.replace(' ','_');
	  texte=texte.replace('\n','_');
	  texte=texte.replace('\t','_');
	  texte=texte.replace('–','_');
	  //texte=texte.replace('�','_');
	  texte=texte.replace('�','_');
	  texte=texte.replace('“','_');
	  texte=texte.replace('‘','_');
	  texte=texte.replace('|','_');
	  texte=texte.replace('‚','_');
	  texte=texte.replace('„','_');
	  texte=texte.replace('·','_');
	  texte=texte.replace('®','_');
	  texte=texte.replace('©','_');
	  texte=texte.replace('~','_');
	  texte=texte.replace('≈','_');
	  texte=texte.replace('‹','_');
	  texte=texte.replace('¬','_');
	  texte=texte.replaceAll(" ", "_" );
	  texte=texte.replace(' ','_');
	  texte=texte.replace('–','_');

	  
	  String[] mots=texte.split("_");
	  //System.out.println("Nombre de termes: "+mots.length);

	  return mots;
	  }

	  /**
	   * Permet de tronquer à 7 caractères un mot
	   */
	   public String troncTerme (String mot) {
	  
	  if (mot.length() > 7) {
			mot = mot.substring(0,7);
			System.out.print(mot+ " - ");
		}
	  
	  return mot;
	   }
	   
	   /**
	    * Teste la présence de mot "vides"
	    */
	   
	   
	   public boolean testMotVide(String mot) {
	 	  if ((Stoptable.get(mot)==null)&&(mot.length()!=0)&&(!mot.equals(" "))) {
	 			if (!(mot.matches("(.*)@(.*)"))&&!(mot.matches("(.*)%(.*)"))&&!(mot.matches("(.*) (.*)"))) {
	 			return true;
	 			}
	 		}
	 		return false;
	 }
	   
	   
	   public String[] lemmatiserTermes(String[] termes) throws Throwable {

			Class stemClass = Class.forName("lemmatisation.frenchStemmer");
		    SnowballProgram stemmer = (SnowballProgram) stemClass.newInstance();
			Method stemMethod = stemClass.getMethod("stem", new Class[0]);
			
			List<String> readerlist = new ArrayList<String>();
			//List<String> resultlist = new ArrayList<String>();
			
			int n =0;
			String mot;
			
			for (int m=0;m<termes.length;m++) {
				mot= termes[m];
				if (testMotVide(mot)){
				readerlist.add(n, mot);
				n++;
				}
			}
			//System.out.println("taille :"+ n + "- "+ readerlist.size());
			
			int repeat = 1;

			String result = "";
			String[] resulttab; 
			
			String temp = "";
			String temp2 = "";
		
			
			Object [] emptyArgs = new Object[0];

			while (readerlist.size()> 0)
			{
				temp = "";
				if (readerlist.size() <6) {	
				for (int j = 0; j < readerlist.size(); j++) {
					temp = temp + " " + readerlist.get(j);
					}				
				}
				else {
					for (int j = readerlist.size() - 6 ; j < readerlist.size(); j++) {
					temp = temp + " " + readerlist.get(j);
					}
				}
				
				//temp = readerlist.get(readerlist.size()-1);
				
					stemmer.setCurrent(temp);
				    for (int i = repeat; i != 0; i--) {
					stemMethod.invoke(stemmer, emptyArgs);
				    }
				    temp2 = stemmer.getCurrent();
				    resulttab= temp2.split(" ");
//				    
//				    System.out.println("temp: "+temp);
//				    System.out.println("result: "+result);
//				    System.out.println("length:"+ resulttab.length);
				    result = resulttab[(resulttab.length-1)] + " "+ result;
				    
				    readerlist.remove(readerlist.size()-1);

			}
			return result.split(" ");
			}
		
	   
	   
	
	
}
